﻿<?php
// WARNING: this will work only in PHP5
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
require_once(DOKU_INC.'phpincludes/ACC_Form_Base.php');

/**
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Stephane Chamberland <dokuwiki@meteokid.ca>
 * @date       2009-08-13
 */
class ACC_DataFile {

    private $filePath = '';
    private $modified = false;
    private $defaultRecord = NULL;
    private $records = NULL;

    /**
     *
     */
    public function __construct($id=NULL,$suffix='accmf') {
        $this->_filePath($id,$suffix);
        $this->modified = false;
        $this->read();
        $this->defaultRecord = array();
    }
    
    //TODO: add a destructor that may (optional) auto save modifications
    
    /**
     *
     */
    public function read($id=NULL,$suffix='accmf',$forced=false) {
        //TODO: add a possibility to read older copies
        if($this->modified && !$forced) return false;
        $this->modified = false;
        if (@file_exists($this->filePath)) {
            $this->records = unserialize(io_readFile($file, false));
        } else {
            $this->records = array();
        }
        return $this->records;
    }

    /**
     *
     */
    public function save($id=NULL,$suffix='accmf') {
        if(!is_null($id)) {
            $this->_filePath($id,$suffix);
            $this->modified = true;
        }
        //TODO: add a possibility to keep older copies
        $stat = true;
        if($this->modified) $stat = io_saveFile($this->filePath, serialize($this->records));
        if($stat) $this->modified = false;
        return $stat;
    }

    /**
     *
     */
    public function get_recs($uid_list=NULL){
        if(is_null($uid_list)) {
            $rec_list = $this->records;
        } else {
            if(!is_array($uid_list)) $uid_list = array($uid_list);
            $rec_list = array();
            foreach($uid_list as $uid) {
                if(isset($this->records[$uid])) $rec_list[$uid] = $this->records[$uid];
            }
        }
        return $rec_list;
    }

    /**
     *
     */
    public function set_defaultRecord($defaultRecord){
        if(!is_array($defaultRecord)) return false;
        $this->defaultRecord = $defaultRecord;
        return true;
    }

    /**
     *
     */
    public function add($record,$noNewKeys=false,$uid=NULL) {
        $values = $this->_preActionValuesMods($values0);
        if(is_null($uid)) {
            $uid = uniqid();
            if(is_array($record) && isset($record['uid'])) $uid = $record['uid'];
            //TODO: if uid already exists... may not allow it... it is an update then
        }
        if(!is_array($record)) {
            $record = $defaultRecord;
        } else {
            //TODO: if($noNewKeys) make sure no new tags is added
            //TODO: check if array_update is a deep update
            $record = array_update($defaultRecord,$record);
        }
        $this->records[$uid] = $record;
        $this->modified = true;
        return $uid;
    }

    /**
     *
     */
    public function update($uid,$record,$replace=false,$noNewKeys=false) {
        if(!is_array($record) ||
           !isset($this->records[$uid])) return false;
        if(!$replace) $record = array_update($this->records[$uid],$record);
        unset($this->records[$uid]);
        $this->modified = true;
        return $this->add($record,$noNewKeys,$uid);
    }

    /**
     *
     */
    public function search_val($values,$uid_list=NULL) {
        return $this->search_fun('ACC_DataFile::ACC_DataFile_search_values',$values,$uid_list);
    }

    /**
     *
     */
    public function search_fun($function,$params=NULL,$uid_list=NULL) {
        #TODO: check if $function exists
        if(is_null($uid_list)) $uid_list = array_keys($this->records);
        $matchlist = array();
        foreach($uid_list as $uid) {
            if(isset($this->records[$uid]) && $function($this->records[$uid],$params)) $matchlist[] = $uid;
        }
        return $matchlist;
    }

    //---- Statics --------------------------------------
    
    /**
     *
     */
    public static function ACC_DataFile_search_values($record,$values){
        $ok = true;
        foreach($values as $k=>$v) {
            if (!isset($record[$uid][$k]) || $record[$uid][$k] != $v) $ok = false;
        }
        return $ok;
    }

    //---- Private --------------------------------------

    /**
     *
     */
    private function _filePath($id=NULL,$suffix='accmf') {
        global $ID;
        //TODO: check for illegal suffixes (.meta, .comments, .php, .html, .cgi, .exe, .pl, .pm, ...)
        if(!$id) $id = $ID;
        $this->filePath = metaFN($id, '.'.$suffix);
    }

}



/**
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Stephane Chamberland <dokuwiki@meteokid.ca>
 * @date       2009-08-13
 */
class ACC_DataFile_KF extends ACC_DataFile {

    var $metaFileSuffix = 'keene';
    var $metaFileid = '__keene';

    var $sortKey = 'date';
    var $selectCriterions = NULL;
    var $checked_uids = NULL;
    var $cancelFields = NULL;
    var $metaFile = NULL;

    /**
     *
     */
    function _sub_list($date0,$date1,$selFunc='',$metaFile=NULL) {
        if(is_null($metaFile) || !is_array($metaFile)) {
            $metaFile = $this->_metaFile_get($this->metaFileid);
            $this->metaFile = $metaFile;
        }
        $d_min = strtotime($date0);
        $d_max = strtotime($date1);
        $subMetaFile = array();
        foreach($metaFile as $uid => $item){
            $d_item = strtotime($item['date']);
            $d_item2 = $d_item + 24*3600*(max(0,$item['nights']-1));
            $keep=false;
            if($d_item2>=$d_min && $d_item<=$d_max){
                $keep=true;
                if($selFunc) $keep=$this->$selFunc($item); #TODO: check if $selFunc exists
                if($keep) $subMetaFile[$uid] = $item;
            }
            #print(date('Y-m-d',$d_item2).' >= '.date('Y-m-d',$d_min).' && '.date('Y-m-d',$d_item).' <= '.date('Y-m-d',$d_max)." : ".$keep."-".$selFunc."\n");
        }
        return $subMetaFile;
    }

    /**
     *
     */
    static static function _selActiveReserv($item) {
        if (in_array(strtolower($item['status']),array('new','confirmed','custodian'))) return true;
        return false;
    }

    /**
     *
     */
    function static _selCustodian($item) {
        if (strtolower($item['status']) == 'custodian') return true;
        return false;
    }

    /**
     *
     */
    function static _selCabin($item) {
        if (strtolower($item['cabin']) == 'cabin') return true;
        return false;
    }

    /**
     *
     */
    function static _selCamping($item) {
        if (strtolower($item['cabin']) == 'camping') return true;
        return false;
    }

    /**
     *
     */
    function static _nbPeople($item) {
        $people = 0;
        foreach(array('people_member','people_guests','people_children') as $k) {
            if (is_numeric($item[$k])) $people += $item[$k];
        }
        return $people;
    }
}
